package view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JPanel;

import model.CalendarViewMode;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import java.util.GregorianCalendar;
import java.util.Observable;
import java.util.Observer;

/**
 * 06-01-2010
 * 
 * Paints the month/year in the monthview
 * Paints the year and weekdays in the weekview.
 * 
 * @author Dave Nieuwenhuijzen
 */
public class CalendarTitleView extends JPanel implements Observer {

	private static final long serialVersionUID = 1L;
	private model.CalendarModel calendarModel;
	private Calendar today = new GregorianCalendar();

	public CalendarTitleView(model.CalendarModel setCalendarModel) {
		setPreferredSize(new Dimension(800, 25));

		calendarModel = setCalendarModel;
	}

	/**
	 * This method is called whenever the observed object is changed.
	 * 
	 * @param observable
	 * @param argument
	 */
	public void update(Observable observable, Object argument) {
		this.revalidate();
		this.repaint();
	}

	/**
	 * Calls the UI delegate's paint method, if the UI delegate is non-null. 
	 */
	public void paintComponent(Graphics graphics) {
		super.paintComponent(graphics);

		if(calendarModel.getCalendarViewMode() == CalendarViewMode.week) {
			paintStyleWeek(graphics);
		} else {
			paintStyleMonth(graphics);
		}
	}
	
	/**
	 * Paints the year and weekdays in the weekview.
	 * 
	 * @param graphics
	 */
	private void paintStyleWeek(Graphics graphics) {
		setBackground(Color.WHITE);
		graphics.setColor(new Color(204, 204, 204, 255));

		// Draw horizontal lines between weekdays
		for (int i = 1; i <= 7; i++) {
			graphics.drawLine(calendarModel.getLimitsHorizontal(i), 0, calendarModel.getLimitsHorizontal(i), getHeight() - 6);
		}

		graphics.drawString(Integer.toString(calendarModel.getSelectedDate().get(Calendar.YEAR)), 5, 16);

		int i = 0;
		Calendar[] weekDays = calendarModel.calculateWeekDays(calendarModel.getSelectedDate());

		for(Calendar date : weekDays) {
			graphics.setColor(Color.BLACK);

			if(util.CalendarConvert.compareDates(today, date) == true) {
				graphics.setColor(new Color(237, 243, 250, 255));
				graphics.fillRect(calendarModel.getLimitsHorizontal(i) + 1, 0, calendarModel.getLimitsHorizontal(i + 1) - calendarModel.getLimitsHorizontal(i) - 1, getHeight() - 4);			
			}

			graphics.setColor(Color.BLACK);

			graphics.drawString(weekDayFormat(date, (calendarModel.getLimitsHorizontal(i + 1) - calendarModel.getLimitsHorizontal(i) - 1)), (calendarModel.getLimitsHorizontal(i) + calendarModel.getLimitsHorizontal(i + 1)) / 2 - 15, 16);

			i++;
		}

		// Separate title and content
		graphics.fillRect(0, getHeight() - 4, getWidth(), getHeight());
	}

	/**
	 * Paints the month/year in the monthview.
	 * 
	 * @param graphics
	 */
	private void paintStyleMonth(Graphics graphics) {
		setBackground(Color.WHITE);

		SimpleDateFormat dateFormat = new SimpleDateFormat("MM/yyyy");

		graphics.drawString(dateFormat.format(calendarModel.getSelectedDate().getTime()), getWidth() / 2, 16);
	}

	/**
	 * Returns the format of the week days depending on the size of the header width.
	 * 
	 * @param date The date to format
	 * @param width The width of the header
	 * @return String Formatted date
	 */
	private String weekDayFormat(Calendar date, int width) {
		SimpleDateFormat dateFormat;

		if(width > 110) {
			// Full day format
			dateFormat = new SimpleDateFormat("EEEE dd/MM");
		} else {
			// Short day format
			dateFormat = new SimpleDateFormat("E dd/MM");
		}

		return dateFormat.format(date.getTime());
	}
}